package com.soft.app.ruanba;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import org.webharvest.definition.ScraperConfiguration;
import org.webharvest.runtime.Scraper;
import org.webharvest.runtime.variables.ListVariable;
import org.webharvest.runtime.variables.NodeVariable;
import org.xml.sax.InputSource;

import com.soft.webharvest.util.ConfigFactory;

public class ExtractJob
{

	private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExtractJob.class);
	private java.lang.String prefix;
	private boolean local;
	private ConfigFactory.Config cconfig;

	public ExtractJob(ConfigFactory.Config cconfig, java.lang.String prefix, boolean local)
	{
		this.cconfig = cconfig;
		this.prefix = prefix;
		this.local = local;
	}

	public void extract()
		throws java.io.IOException
	{
		java.lang.String webharvestConfig = cconfig.getProperty((new StringBuilder()).append(prefix).append("webharvestConfig").toString());
		java.lang.String destDir = cconfig.getProperty((new StringBuilder()).append(prefix).append("destDir").toString());
//		java.lang.String maxloops = cconfig.getProperty((new StringBuilder()).append(prefix).append("maxloops").toString());
		log.info("webharvestConfig:{}", webharvestConfig);
//		log.info("destDir:{}", destDir);
//		if (org.apache.commons.lang.StringUtils.isBlank(webharvestConfig) || org.apache.commons.lang.StringUtils.isBlank(destDir))
		if (org.apache.commons.lang.StringUtils.isBlank(webharvestConfig))
		{
			log.error("缺少必要的参数");
			java.lang.System.exit(-1);
		}
		org.webharvest.definition.ScraperConfiguration config = null;
		if (local)
		{
			java.io.File configFile = new File(webharvestConfig);
			if (!configFile.exists())
			{
				log.error("{}文件不存在", configFile.getAbsolutePath());
				java.lang.System.exit(-1);
			}
			config = new ScraperConfiguration(configFile);
		} else
		{
			InputStream is = new FileInputStream(webharvestConfig);
			config = new ScraperConfiguration(new InputSource(is));
		}
		org.webharvest.runtime.Scraper scraper = new Scraper(config, destDir);
//		scraper.addVariableToContext("maxloops", maxloops);
		java.util.List list = new ArrayList();
		org.webharvest.runtime.variables.ListVariable lv = new ListVariable();
//		lv.addVariable(new NodeVariable(java.net.URLEncoder.encode("广东", "gbk")));
//		lv.addVariable(new NodeVariable(java.net.URLEncoder.encode("上海", "gbk")));
//		lv.addVariable(new NodeVariable(java.net.URLEncoder.encode("北京", "gbk")));
//		lv.addVariable(new NodeVariable(9));
//		scraper.addListVariableToContext("cateArray", lv);
//		scraper.setDebug(false);
		scraper.execute();
	}

}
